close all;
clear;

%% 
% product_name = 'GF3_178100';
product_name = 'LT1_210226';

%% 读取 SAR
mli_path = ['pipe01_simsar/results/', product_name, '/', product_name  , '.mli'];
mli_par_path = ['pipe01_simsar/results/', product_name, '/', product_name  , '.mli_par'];

mli_data = gamma.readMli(mli_path, mli_par_path);
[Na, Nr] = size(mli_data);

% tmp = hsUtils.stretchImageByHist(mli_data, 10000, "HistHalfPerc", 0.05);
% tmp = single(tmp);

tmp_mli = mli_data;
thre = mean(tmp_mli(:)) * 3;
tmp_mli(tmp_mli > thre) = thre;
figure;
imagesc(tmp_mli);
axis equal;
axis tight;
title('multilook SAR');

%% 读取 DEM
dem_path = ['pipe01_simsar/results/', product_name, '/', product_name, '_resamp.dem'];
dem_par_path = ['pipe01_simsar/results/', product_name, '/', product_name, '_resamp.dem_par'];

[dem_data, dem_param] = gamma.readDem(dem_path, dem_par_path);

figure;
imagesc(dem_data);
axis equal;
axis tight;
title('DEM');

%% 读取地理编码表
lkt_path = ['pipe01_simsar/results/', product_name, '/', product_name, '.lkt'];

[rid_grid0, aid_grid0] = gamma.readLKT(lkt_path, dem_par_path);
rid_grid0 = rid_grid0 + 1;
aid_grid0 = aid_grid0 + 1;

mask = (rid_grid0 >= 1 & rid_grid0 <= Nr & aid_grid0 >= 1 & aid_grid0 <= Na);
rid_grid0(~mask) = nan;
aid_grid0(~mask) = nan;

figure;
subplot(121);
imagesc(rid_grid0);
title('range');
axis equal;
axis tight;
subplot(122);
imagesc(aid_grid0);
title('azimuth');
axis equal;
axis tight;

%%
fine_lkt_path = ['pipe01_simsar/results/', product_name, '/', product_name, '.fine_lkt'];

[rid_grid1, aid_grid1] = gamma.readLKT(fine_lkt_path, dem_par_path);
rid_grid1 = rid_grid1 + 1;
aid_grid1 = aid_grid1 + 1;

mask = (rid_grid1 >= 1 & rid_grid1 <= Nr & aid_grid1 >= 1 & aid_grid1 <= Na);
rid_grid1(~mask) = nan;
aid_grid1(~mask) = nan;

figure;
subplot(121);
imagesc(rid_grid1);
title('range fine');
axis equal;
axis tight;
subplot(122);
imagesc(aid_grid1);
title('azimuth fine');
axis equal;
axis tight;

%%
gtc_img_old = hsUtils.gridInterp2d(tmp_mli, 1, 1, 1, 1, rid_grid0, aid_grid0);

% gtc_img_old_1 = hsUtils.stretchImageByHist(gtc_img_old, 10000, "HistHalfPerc", 0.001, "Show", ture);
% thre = mean(gtc_img_old(~isnan(gtc_img_old)));
% gtc_img_old(gtc_img_old > thre) = thre;

gtc_img_fine = hsUtils.gridInterp2d(tmp_mli, 1, 1, 1, 1, rid_grid1, aid_grid1);

% gtc_img_fine_1 = hsUtils.stretchImageByHist(gtc_img_fine, 10000, "HistHalfPerc", 0.0001, "Show", ture);
% thre = mean(gtc_img_fine(~isnan(gtc_img_fine)));
% gtc_img_fine(gtc_img_fine > thre) = thre;


figure;
imagesc(gtc_img_old);
axis equal;
axis tight;
title("gtc old");
colormap(hs.img.color.cmap.cm_L2)
% xlim([800 1700])
% ylim([1300 2200])

figure;
imagesc(gtc_img_fine);
axis equal;
axis tight;
title("gtc fine");
colormap(hs.img.color.cmap.cm_L2)
% xlim([800 1700])
% ylim([1300 2200])

